package Controller;

import java.awt.EventQueue;
import java.util.HashMap;
import java.util.Map;

import View.LoginView;
import View.MainView;
import View.MyView;

public class MainController {	
	public static final String LOGIN_VIEW_NAME = "Login";
	public static final String MAIN_VIEW_NAME = "Main";
	
	private static Map<String, MyView> mapView = new HashMap<String, MyView>();
	
	public static void main(String[] args) {
		
		OpenLoginView();
	}
	
	public static void OpenLoginView() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {					
					LoginView window = new LoginView(LOGIN_VIEW_NAME);
					mapView.put(window.getName(), window);
					window.getFrame().setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	
	public static void OpenMainView() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {					
					MainView window = new MainView(MAIN_VIEW_NAME);;
					mapView.put(window.getName(), window);
					window.getFrame().setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	/**
	 * Get MyView by its name.
	 * @param viewName Get from "MainController." or from method getName() of MyView instance
	 * @return MyView Which MyView instance exists (opening).
	 */
	public static MyView GetViewByName(String viewName)
	{
		return mapView.get(viewName);
	}
	
	/**
	 * Close View
	 * @param viewName Get from "MainController." or from method getName() of MyView instance
	 */
	public static void CloseView(String viewName)
	{
		MyView view = GetViewByName(viewName);
		
		if(view != null)
		{
			//remove from list view
			mapView.remove(view);	
			// dispose view
			view.getFrame().dispose();			
		}
		else
		{
			// This view doesn't exist
		}
	}
}
